package com.jt.controller;

import com.jt.pojo.UserPojo;
import com.jt.service.UserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/findAll")
    public String findAll(Model model) {
        List<UserPojo> userList = userService.findAll();
        model.addAttribute("userList", userList);
        return "index";
    }

    @GetMapping("/findById/{id}")
    public String findById(@PathVariable int id, Model model) {
        UserPojo user = userService.findById(id);
        model.addAttribute("user", user);
        return "update";
    }

    @PostMapping("/updateById")
    public String updateById(UserPojo userPojo, Model model) {
        String msg = userService.updataById(userPojo);
        model.addAttribute("msg", msg);
        return this.findAll(model);
    }

    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable int id, Model model) {
        String msg = userService.deleteById(id);
        model.addAttribute("msg", msg);
        return this.findAll(model);
    }
}
